package com.boot;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class TestSecMain {

	public static void main(String[] args) throws Exception  {
		
		final int MAX_LEN = 10240;
		
		String classFile = "E:\\tmp\\com\\boot\\Simple.class";
		String enclassFile = "E:\\tmp\\com\\boot\\Simple.sec.class";
		String bakclassFile = "E:\\tmp\\com\\boot\\Simple.bak.class";
		String orgclassFile = "E:\\tmp\\com\\boot\\Simple.org.class";
		
		FileInputStream in = new FileInputStream(classFile);
		int i = 0, j = 0;
		byte[] buff = new byte[MAX_LEN];
		//byte[] outBuffer = new byte[1024];
//		StringBuilder sb = new StringBuilder();
		
		while ((i = in.read(buff, 0, MAX_LEN)) != -1){
//			sb.append(new String(buff, 0 , i));
			j = i;
			System.out.println(".");
		}
		
		byte[] outBuffer = new byte[j];
		
		for (int k = 0; k < j; k++) {
			outBuffer[k] = buff[k];
		}
		
		byte[] decbuff = ClassSecurity.encode(outBuffer);
		
		System.out.println("load class bytes: " + j );
		
		in.close();
		
		//encode the class file
		FileOutputStream out = new FileOutputStream(enclassFile);
		out.write(decbuff);
		out.close();
		
		//backup the class file
		out = new FileOutputStream(bakclassFile);
		out.write(outBuffer);
		out.close();

		//decode the class file to check the byte data
		out = new FileOutputStream(orgclassFile);
		out.write(ClassSecurity.decode(decbuff));
		out.close();
		
		
	}

}
